package com.sun.view;

import org.springframework.core.Ordered;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

import java.util.Locale;

/**
 * 实现视图解析器接口
 * 实现ordered的接口，为视图解析器排序
 * @author sun
 */
public class MyViewResolver implements ViewResolver, Ordered {

    private Integer order;
    /**
     * 根据视图名返回视图
     * <p>Note: To allow for ViewResolver chaining, a ViewResolver should
     * return {@code null} if a view with the given name is not defined in it.
     * However, this is not required: Some ViewResolvers will always attempt
     * to build View objects with the given name, unable to return {@code null}
     * (rather throwing an exception when View creation failed).
     *
     * @param viewName name of the view to resolve
     * @param locale   the Locale in which to resolve the view.
     *                 ViewResolvers that support internationalization should respect this.
     * @return the View object, or {@code null} if not found
     * (optional, to allow for ViewResolver chaining)
     * @throws Exception if the view cannot be resolved
     *                   (typically in case of problems creating an actual View object)
     */
    @Override
    public View resolveViewName(String viewName, Locale locale) throws Exception {
        //获取以sun:开头的视图名，如果不符合返回null，让其他视图解析器解析
        if(viewName.startsWith("sun:")){
            return new MyView();
        }

        return null;
    }

    @Override
    public int getOrder() {
        return this.order;
    }

    public void setOrder(Integer order){
        this.order=order;
    }
}
